Skip to content

Method: setOperations(GenUserClass, Context)

1: package de.fhdw.wtf.generator.writer.writer;
2:
3: import java.io.File;
4: import java.util.ArrayList;
5: import java.util.Collection;
6:
7: import org.apache.velocity.context.Context;
8:
9: import de.fhdw.wtf.generator.java.generatorModel.GenClass;
10: import de.fhdw.wtf.generator.java.generatorModel.GenJavaOperation;
11: import de.fhdw.wtf.generator.java.generatorModel.GenUserClass;
12:
13: /**
14: * File writer to generate code for {@line GenUserClass}es
15: */
16: public class JavaClassFileWriter extends ClassClassFileWriter {
17:         
18:         public JavaClassFileWriter(final boolean fullyQualified) {
19:                 super(TEMPLATE_FILE_NAME, fullyQualified);
20:         }
21:         
22:         private static final String TEMPLATE_FILE_NAME = "de/fhdw/wtf/generator/templates/modelclass.vm";
23:         
24:         private void setUpContext(final GenUserClass c, final Context currentContext) {
25:                 this.setAttributes(c, currentContext);
26:                 this.setOperations(c, currentContext);
27:         }
28:         
29:         private void setOperations(final GenUserClass c, final Context currentContext) {
30:                 currentContext.put(PARSED_OPERATIONS_KEY, this.getParsedOperations(c.getOperations()));
31:                 final Collection<GenJavaOperation> operationsWithSimple = new ArrayList<>();
32:                 operationsWithSimple.addAll(c.getOperations());
33:                 operationsWithSimple.addAll(c.getConstructors());
34:                 currentContext.put(SIMPLE_OPERATIONS_KEY, this.getSimpleOperations(operationsWithSimple));
35:         }
36:         
37:         /**
38:          * Writes the file for the given {@link de.fhdw.wtf.generator.java.generatorModel.GenClass} c.
39:          *
40:          * @param c
41:          */
42:         public void writeJavaClass(final GenUserClass c, final File rootdir) {
43:                 this.writeClassClass(c);
44:                 this.setUpContext(c, this.getCurrentContext());
45:                 this.writeToFile(c, rootdir);
46:         }
47:         
48:         /**
49:          * Writes the given {@link GenUserClass} c to a String.
50:          *
51:          * @param c
52:          * The class to write.
53:          * @return A String representation of the GenUserClass in java-syntax.
54:          */
55:         public String writeJavaClassToString(final GenUserClass c) {
56:                 this.writeClassClass(c);
57:                 this.setUpContext(c, this.getCurrentContext());
58:                 return this.writeToString();
59:         }
60:         
61:         /**
62:          * Sets all values for the attribute declaration of {@link de.fhdw.wtf.generator.java.generatorModel.GenClass} c to
63:          * the {@link Context}.
64:          *
65:          * @param c
66:          * @param currentContext
67:          */
68:         private void setAttributes(final GenUserClass c, final Context currentContext) {
69:                 currentContext.put(ATTRIBUTES_KEY, this.getAttributes(c.getAttributes()));
70:         }
71:         
72:         @Override
73:         public String getStringContent(final GenClass c) {
74:                 final GenUserClass cAsGenUserClass = (GenUserClass) c;
75:                 this.writeClassClass(cAsGenUserClass);
76:                 this.setUpContext(cAsGenUserClass, this.getCurrentContext());
77:                 return this.writeToString();
78:         }
79: }